<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kardex extends CI_Controller {

	public function index()
	{
	   $this->load->model('Kardex_model');
           $this->load->model('warehouses_xproducts_model');
           $this->load->helper(array('form', 'url'));            
	    $code = null;
	    $id_warehouse = null;
	    $desde = null;
           $hasta =null;
            
	    if ($this->input->post('code')) {
	        $code = $this->input->post('code');
	    }
	    if ($this->input->post('warehouses')) {
	        $id_warehouse = $this->input->post('warehouses');
	    }
	    if ($this->input->post('desde')) {
	        $desde = $this->input->post('desde');
	    }
            if ($this->input->post('hasta')) {
	        $hasta = $this->input->post('hasta');
	    }
            $warehouses = $this->warehouses_xproducts_model->getAll();
	    if ($code != null || $id_warehouse != null || $desde != null || $hasta != null) {
	        $kardex = $this->Kardex_model->search($code, $id_warehouse, $desde, $hasta);
	    }
            
            $vars['code'] = $code;
            $vars['warehouses'] = $warehouses;
            $vars['id_warehouse'] = $id_warehouse;
            $vars['content_view'] = 'kardex_search';   
	    $vars['menu'] = 'warehouses';
            $vars['desde'] = $desde;
            $vars['hasta'] = $hasta; 
            //$vars['kardex'] = $kardex;
            //$this->load->view('kardex_by_product', $vars);
             
	    $this->load->view('template', $vars);
	}
        
        
        public function products() {
        $code = null;
        $description = null;
        $this->load->model('Products_model');        
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        $products = array();
        if ($code != null || $description != null) {
            $products = $this->Products_model->search($code, $description);            
        }
        $vars['products'] = $products;
        //$vars['code'] = $code;        
        //$vars['description'] = $description;
        $this->load->view('products_list_kardex', $vars);
    }
        public function reporte() {
            
           $this->load->model('Kardex_model');
           $this->load->model('warehouses_xproducts_model');
           $this->load->model('Sales_model');
           $this->load->model('Warehouses_model');
           $this->load->model('Products_model');
           
           $this->load->helper(array('form', 'url'));            
	   $code = null;
	   $id_warehouse = null;
	   $desde = null;
           $hasta =null;
           
           
	    if ($this->input->post('code1')) {
	        $code = $this->input->post('code1');
	    }
	    if ($this->input->post('warehouses')) {
	        $id_warehouse = $this->input->post('warehouses');
	    }
	    if ($this->input->post('desde')) {
	        $desde = $this->input->post('desde');
                $datei = $this->input->post('desde');
                $date = str_replace('/', '-', $datei);
                $datedesde = date("Y-m-d", strtotime($date) );
	    }
            if ($this->input->post('hasta')) {
	        $hasta = $this->input->post('hasta');
                $datei = $this->input->post('hasta');
                $date = str_replace('/', '-', $datei);
                $datehasta = date("Y-m-d", strtotime($date) );
	    }
            
            $product = $this->Products_model->find($code);
            $namewarehouse = $this->Warehouses_model->find($id_warehouse);
            $warehouses = $this->warehouses_xproducts_model->getAll();
	    if ($code != null || $id_warehouse != null || $desde != null || $hasta != null) {
	        $kardex = $this->Kardex_model->search($code, $id_warehouse, $datedesde, $datehasta);
	    }
            $listar_stock_product_almacen = $this->Sales_model->select_almacen_productos($code,$id_warehouse);
             foreach ($listar_stock_product_almacen as $lis ){
            $scajas = $lis->boxes;
            $spaquetes = $lis->packs;
            $sunidades = $lis->Units;
             }
             
            $num_filas= count($kardex);            
            
             
            if($num_filas>0)
            {
            $vars['scajas'] = $scajas;
            $vars['spaquetes'] = $spaquetes;
            $vars['sunidades'] = $sunidades;
            $vars['code'] = $code;
            $vars['warehouses'] = $warehouses;
            $vars['id_warehouse'] = $id_warehouse;
            $vars['content_view'] = 'kardex_search';   
	    $vars['menu'] = 'warehouses';
            $vars['desde'] = $desde;
            $vars['hasta'] = $hasta; 
            $vars['kardex'] = $kardex;
            $vars['namewarehouse'] = $namewarehouse;
            $vars['product'] = $product;
            $this->load->view('kardex_by_product', $vars);
            }
            else {
                return 0;
            }
	    //$this->load->view('template', $vars);
    }
		
}